// pages/goodsmanage/goodsmanage.js
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
var api = require('../../config/api.js')
var comm = require("../../utils/comm.js")
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    inputShowed: false,
    inputVal: "",

    tabs: ["商铺列表", "新增商铺",],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,

    toast1Hidden: true,
    modalHidden: true,
    modalHidden2: true,
    notice_str: '',

    files: [],

    myAgentList: {
      hasMore: true,
      list: [],
      listSize: 0
    },
    startNum: 1, //页数

    search: false, //判断模糊查询方法

  },

  bindAccountChange: function (e) {
    console.log('picker account 发生选择改变，携带值为', e.detail.value);

    this.setData({
      accountIndex: e.detail.value
    })
  },

  mang: function (e) {
    wx.navigateTo({
      url: '/pages/shopset/shopset.wxssshopinfo?id=' + e.currentTarget.id,
    })
  },

  toast1Change: function (e) {
    this.setData({
      toast1Hidden: true
    });
  },

  // 搜索
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false,
      search: false
    });
    this.onShow();
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },

  searInput: function (e) {
    var _that = this
    console.log("inputVal=" + _that.data.inputVal)
    wx.showLoading({
      title: '搜索中',
    })
    if (_that.data.inputVal.length != '') {
      wx.request({
        url: api.BuesList + '?current=1&size=5',
        data: {
          deptName: _that.data.inputVal,
          deptType: '4' 
        },
        method: 'POST',
        header: {
          'Accept': 'application/json',
          'ACCESS-TOKEN': wx.getStorageSync('token')
        },
        success: function (res) {
          console.log(res.data)
          wx.hideLoading();
          if (res.data.code == '1') {
            var listSize = res.data.obj.records.length;
            console.log('listSize=' + listSize);
            var newData = _that.data.myAgentList.list;
            newData = res.data.obj.records;
            var total1 = res.data.obj.total;
            var newList = {};
            //当长度小于10表示没有下页数据了
            if (listSize < 5) {
              newList.hasMore = false;
              wx.showToast({
                title: '已加载全部列表',
                icon: 'none'
              })
            } else {
              newList.hasMore = true;
            }
            newList.list = newData;
            console.log('newData=' + newData);
            newList.listSize = listSize;
            _that.setData({
              myAgentList: newList,
              total: total1,
              search: true
            });

            console.log('myAgentList=' + _that.data.myAgentList);

            _that.setData({
              startNum: 2
            })
          } else {
            // util.msg("错误", res.data.msg)
            console.log("网络通，数据不成功")
          }
        },
        fail: function (res) {
          wx.hideLoading();
          console.log(res.data)
          // util.msg("错误", "通讯失败")
        }
      })

    } else {
      wx.showToast({
        title: "输入内容不能为空",
        icon: 'none',
        duration: 2000
      });
    }

  },

  //上拉获取列表
  hotActivityLoadMore: function (e) {
    var that = this
    var search = that.data.search
    if (search) {
      that.listPage({ deptName: that.data.inputVal, deptType: '4' })
    } else if (!search) {
      that.listPage({ deptType: '4' })
    }

  },

  //分页查询列表
  listPage: function (data = {}) {
    var _that = this;
    //判断是否还有下一页
    if (_that.data.myAgentList.hasMore == true) {
      wx.showLoading({
        title: '加载中',
      })
      var pageNum = _that.data.startNum
      var _url = '?current=' + pageNum + '&size=5';

      comm.request(api.StorePagList + _url, data, "POST").then(function (res) {
        wx.hideLoading();
        if (res.code == '1') {
          var listSize = res.obj.records.length;
          console.log('listSize=' + listSize);
          var newData = _that.data.myAgentList.list;
          var lis = res.obj.records;
          var total1 = res.obj.total;
          //concat是在原有基础上进行增加
          newData = newData.concat(lis);
          var newList = {};
          //当长度小于5表示没有下页数据了
          if (listSize < 5) {
            wx.showToast({
              title: "已加载全部列表",
              icon: 'none',
              duration: 2000
            });
            newList.hasMore = false;
          } else {
            newList.hasMore = true;
          }
          newList.list = newData;
          newList.listSize = listSize;
          _that.setData({
            myAgentList: newList,
            total: total1
          });

          _that.setData({
            startNum: pageNum + 1 //页数加1
          })
        } else {
          // util.msg("错误", res.data.msg)
          console.log("网络通，数据不成功")
        }

      }).catch(function (res) {

        console.log("请求失败！")
      })
    } else {
      wx.showToast({
        title: "已加载全部商品",
        icon: 'none',
        duration: 2000
      });
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });



  },

  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },


  formSubmit: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否确认提交',
      confirmColor: "#43CBC9",
      success: (res) => {
        wx.showLoading({
          title: '加载中',
        })

        if (res.confirm) {
          console.log('for发生了submit事件，携带数据为：', e.detail.value);
          var reg = /^[0-9a-zA-Z]+$/;
          var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;

          if (e.detail.value.deptName.length == 0) {
            wx.showToast({
              title: "店铺名称不能为空！",
              icon: 'none',
              duration: 2000
            });
            return false;
          } else if (e.detail.value.phone.length == 0) {
            wx.showToast({
              title: "手机号码不能为空!",
              icon: 'none',
              duration: 2000
            });
            return false;
          } else if (e.detail.value.phone.length != 11) {
            wx.showToast({
              title: "手机号长度不正确!",
              icon: 'none',
              duration: 2000
            });
            return false;
          } else if (!myreg.test(e.detail.value.phone)) {
            wx.showToast({
              title: '手机号格式有误！',
              icon: 'none',
              duration: 1500
            })
            return false;
          } else if (e.detail.value.address.length == 0) {
            wx.showToast({
              title: "地址不能为空!",
              icon: 'none',
              duration: 2000
            });
            return false;
          }

          wx.request({
            url: api.StoreAdd,
            data: {
              deptName: e.detail.value.deptName,
              phone: e.detail.value.phone,
              address: e.detail.value.address
            },
            method: 'POST',
            header: {
              'Accept': 'application/json',
              'ACCESS-TOKEN': wx.getStorageSync('token')
            },
            success: (res) => {
              wx.showLoading({
                title: '加载中',
              })
              if (res.data.success) {
                wx.showModal({
                  title: '提示',
                  content: res.data.msg,
                  confirmColor: "#43CBC9",
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      that.setData({
                        activeIndex: 0,
                        deptName: '',
                        phone: '',
                        address: '',
                      })
                      that.onLoad()
                    }
                  }
                });
                wx.hideLoading();
              }
            },
            fail: function () {
              wx.hideLoading();
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 3000
              });
            },
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
          wx.hideLoading();
        }
      },
    })
  },

  formReset: function () {
    this.setData({
      modalHidden2: false,
      showDistpicker: false,
      provinceSelIndex: null,
    });
  },

  //弹出确认框  
  modalTap: function (e) {
    this.setData({
      modalHidden: true
    })
  },
  confirm_one: function (e) {
    console.log(e);
    this.setData({
      modalHidden: true,
      toast1Hidden: false,
      notice_str: '提交成功'
    });
  },
  cancel_one: function (e) {
    console.log(e);
    this.setData({
      modalHidden: true,
      toast1Hidden: false,
      notice_str: '取消成功'
    });
  },
  //弹出提示框  
  modalTap2: function (e) {
    this.setData({
      modalHidden2: false
    })
  },
  modalChange2: function (e) {
    this.setData({
      modalHidden2: true
    })
  },

  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad();

    var _that = this
    var _url = api.BuesList + '?current=1&size=5';
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: _url,
      data: {
        deptType: '4',
      },
      method: 'POST',
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        if (res.data.code == '1') {
          var listSize = res.data.obj.records.length;
          console.log('listSize=' + listSize);
          var newData = _that.data.myAgentList.list;
          newData = res.data.obj.records;
          var total1 = res.data.obj.total;
          var newList = {};
          //当长度小于10表示没有下页数据了
          if (listSize < 5) {
            newList.hasMore = false;
          } else {
            newList.hasMore = true;
          }
          newList.list = newData;
          console.log('newData=' + newData);
          newList.listSize = listSize;
          _that.setData({
            myAgentList: newList,
            total: total1
          });

          console.log('myAgentList=' + _that.data.myAgentList);

          _that.setData({
            startNum: 2
          })
        } else {
          // util.msg("错误", res.data.msg)
          console.log("网络通，数据不成功")
        }
      },
      fail: function (res) {
        wx.hideLoading();
        console.log(res.data)
        // util.msg("错误", "通讯失败")
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})